// pages/order/order.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab: [{
        name: "全部"
      },
      {
        name: "待付款"
      },
      {
        name: "待发货"
      },
      {
        name: "待收货"
      },
      {
        name: "已完成"
      },

    ], //选项卡
    current: 0,
    index: 0,
    shoplist: ''
  },
  // 切换
  chooseTab: function(e) {
    console.log(e)
    var index = e.currentTarget.dataset.i
    let t = this,
      i = e.target.dataset.i;
    t.setData({
      current: i,
      index: index
    })
    this.getAllshoporder(index)
  },
  // 删除
  delete: function(e) {
    console.log(e)
    var shoplist = this.data.shoplist
    console.log(shoplist)
    let index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id
    console.log(id)
    shoplist.splice(index, 1)
    this.Delteorders(id)
    this.setData({
      shoplist: shoplist,
    })
    console.log(this.data.shoplist)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
  },
  chakwliu: function(e) {
    var that = this
    var gsname = e.currentTarget.dataset.gsname
    var orders = e.currentTarget.dataset.orders
    var shname = e.currentTarget.dataset.shname
    var spid = e.currentTarget.dataset.spid
    var com = e.currentTarget.dataset.com
    var num = e.currentTarget.dataset.num
    wx.navigateTo({
      url: '/hyb_yl/userCommunicate/pages/load/load?gsname=' + gsname + '&orders=' + orders + '&shname=' + shname + '&spid=' + spid + '&com=' + com + '&num=' + num,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getAllshoporder()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if (currPage.data.plstate == 1) {
      that.getAllshoporder();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  getAllshoporder() {
    var that = this
    var openid = wx.getStorageSync('openid')
    var index = that.data.index
    app.util.request({
      'url': 'entry/wxapp/Goods.allshoporder',
      data: {
        index: index,
        openid: openid,
      },
      success: function(res) {
        console.log(res)
        that.setData({
          shoplist: res.data
        })
      }
    })
  },
  Delteorders: function(id) {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/Goods.delete',
      data: {
        id: id
      },
      success: function(res) {
        console.log(res)
      }
    })
  },
  //支付
  zhifu: function(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var orderNo = e.currentTarget.dataset.order
    var money = e.currentTarget.dataset.money
    console.log(id, orderNo)
    app.util.request({
      'url': 'entry/wxapp/Goods.pay',
      header: {
        "Content-Type": "application/xml"
      },
      method: "GET",
      data: {
        openid: wx.getStorageSync('openid'),
        z_tw_money: money,
        orderNo: orderNo
      },
      success: function(res) {
        console.log(res)
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr, //字符串随机数
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          success: function(res) {
            console.log(res)
          }
        })
      }
    })

  },
  enter: function(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var totalMoney = e.currentTarget.dataset.totalMoney
    wx.showModal({
      content: '是否确认收货',
      success: function(resa) {
        if (resa.cancel) {
          console.log('用户点击了取消')
        } else {
          //点击确定
          app.util.request({
            'url': 'entry/wxapp/Goods.upstatus',
            data: {
              id: id,
              orderStatus: 2
            },
            success: function(res) {
              wx.showToast({
                title: '您已收货',
                icon: 'none',
                duration: 3000
              })
              that.getAllshoporder()
            }
          })
        }
      }

    })

  },
  goumai: function(e) {
    var that = this
    var sid = e.currentTarget.dataset.sid
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/product/product?sid=' + sid,
    })
  },
  orderinfo: function(e) {
    var that = this
    var sid = e.currentTarget.dataset.sid
    var id = e.currentTarget.dataset.id
    var status = e.currentTarget.dataset.status
    if (status == '-2'){
      wx.navigateTo({
        url: '/hyb_yl/backstageServices/pages/drupmespay/drupmespay?pay=no',
      })
    }else{
      wx.navigateTo({
        url: '/hyb_yl/userCommunicate/pages/sure_Details/sure_Details?sid=' + sid + '&id=' + id
      })
    }
  },
  pingjia: function(e) {
    var that = this
    console.log(e)
    var sid = e.currentTarget.dataset.sid
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/hyb_yl/userCommunicate/pages/addComment/addComment?sid=' + sid + '&id=' + id,
    })
  },
  //提醒发货
  tixing() {
    var that = this

  }
})